package com.agung.wonogirimaps.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.agung.wonogirimaps.BuildConfig;
import com.agung.wonogirimaps.R;
import com.agung.wonogirimaps.entity.Lokasi;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap.CompressFormat;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Utils
{

	public static final String				API				= "http://wonogirimaps.tk/service/service.php";
	public static final String				API_CATEGORY	= API
																	+ "?get=category";
	public static final String				API_LOCATION	= API
																	+ "?get=location&cat_id=";
	public static final String				API_HOTEL		= API
																	+ "?get=location&cat_id=3";
	public static final String				API_RUMAH_MAKAN	= API
																	+ "?get=location&cat_id=4";
	public static final String				API_SEARCH		= API
																	+ "?get=search&term=";
	public static final String				API_CEK_EMAIL	= API
																	+ "?get=email&name=";

	public static final String				API_RATE		= API + "?get=rate";

	private static final String				PREF_NAME		= "wonogiri_prefs";
	private static final String				USERLOGIN		= "login_user";
	public static final String				EMAIL			= "email_user";
	public static final String				NAME			= "nama_user";
	private static SharedPreferences		sh;

	public static ImageLoaderConfiguration	imgLoaderConf;
	public static DisplayImageOptions		imgOptions;

	public static String API_CEK_USER_RATE(String email, int idLocation)
	{
		return API + "?get=find_user&email=" + email + "&location="
				+ idLocation;
	}

	public static String API_REGIS_USER(String email, String nama)
	{
		return API + "?get=register&email=" + email + "&nama=" + nama;
	}

	private static File getCacheDir(Context ctx)
	{
		return StorageUtils.getCacheDirectory(ctx);
	}

	/**
	 * configurasi untuk imageloader
	 * 
	 * @param ctx
	 *            Context
	 * @return imgLoaderConfiguration
	 */
	public static ImageLoaderConfiguration getImgConfig(Context ctx)
	{
		imgLoaderConf = new ImageLoaderConfiguration.Builder(ctx)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(getCacheDir(ctx)))
				.writeDebugLogs().build();
		return imgLoaderConf;
	}

	/**
	 * image options untuk imageloader
	 * 
	 * @return imgOptions
	 */
	public static DisplayImageOptions getImgOpt()
	{
		imgOptions = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher).cacheOnDisc(true)
				.cacheInMemory(true).delayBeforeLoading(10).build();
		return imgOptions;
	}

	/**
	 * 
	 * // get=rate&user=restu@gmail.com&lokasi=1&nilai_rate=2&comment=a
	 * 
	 * @param user
	 * @param lokasi
	 * @param rate
	 * @param komentar
	 * @return
	 */
	public static String API_RATE(String user, int lokasi, float rate,
			String komentar)
	{
		return API_RATE + "&user=" + user + "&lokasi=" + lokasi
				+ "&nilai_rate=" + rate + "&comment=" + komentar;
	}

	/**
	 * Method untuk debugging aplikasi
	 * 
	 * @param TAG
	 *            class dimana debugging dijalankan
	 * @param msg
	 *            pesan yang akan ditampilkan
	 */
	public static void TRACE(String TAG, String msg)
	{
		if (BuildConfig.DEBUG)
		{
			Log.d(TAG, msg);
		}
	}

	/**
	 * cek user login
	 * 
	 * @param con
	 *            context
	 * @return true if userlogi
	 */
	public static boolean isUserLogin(Context con)
	{
		sh = con.getSharedPreferences(PREF_NAME, 0);

		return sh.getBoolean(USERLOGIN, false);
	}

	/**
	 * Set user login
	 * 
	 * @param con
	 *            Context
	 * @param isLogin
	 *            login parameter (true|false)
	 */
	public static void setUserLogin(Context con, boolean isLogin)
	{
		sh = con.getSharedPreferences(PREF_NAME, 0);
		SharedPreferences.Editor editor = sh.edit();
		editor.putBoolean(USERLOGIN, isLogin);
		editor.commit();
	}

	/**
	 * 
	 * @param con
	 *            Context
	 * @param saveTo
	 *            disimpan ke ?
	 * @param data
	 *            data yang disimpan
	 */
	public static void setDataStringtoSP(Context con, String saveTo, String data)
	{
		sh = con.getSharedPreferences(PREF_NAME, 0);
		SharedPreferences.Editor editor = sh.edit();
		editor.putString(saveTo, data);
		editor.commit();
	}

	/**
	 * 
	 * @param con
	 *            Context
	 * @param getWhat
	 *            yang diambil apa.?
	 * @return data
	 */
	public static String getDataStringSP(Context con, String getWhat)
	{
		sh = con.getSharedPreferences(PREF_NAME, 0);
		return sh.getString(getWhat, "null");
	}

	/**
	 * Method yang digunakan untuk sorting data lokasi berdasarkan rate,
	 * diurutkan dari yang paling besar
	 * 
	 * @param list
	 *            List Lokasi
	 */
	public static void sortingLokasiDESC(List<Lokasi> list)
	{
		Collections.sort(list, new Comparator<Lokasi>() {
			@Override
			public int compare(Lokasi lhs, Lokasi rhs)
			{

				return (lhs.getRate() > rhs.getRate() ? -1
						: (rhs.getRate() == lhs.getRate() ? 0 : 1));
			}
		});

	}

	@SuppressWarnings("static-access")
	public static boolean checkingGPS(Context context)
	{
		LocationManager locationManager = (LocationManager) context
				.getSystemService(context.LOCATION_SERVICE);

		return (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) ? true
				: false;

	}

	public static boolean isConnectedToInternet(Context ctx)
	{
		ConnectivityManager conn = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conn != null)
		{
			NetworkInfo[] info = conn.getAllNetworkInfo();
			if (info != null)
			{
				for (int i = 0; i < info.length; i++)
				{
					if (info[i].getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}
				}
			}
		}
		return false;
	}

	public static String urlEncode(String data)

	{
		try
		{
			String query = URLEncoder.encode(data, "utf-8");
			return query;

		} catch (UnsupportedEncodingException e)
		{
			//
			return "";
		}

	}

}
